 /**
 * The Settings Stack stores the paths and path tokens for external data.
 */
package uk.co.revisual.pivot.proxies
{
	import org.puremvc.as3.multicore.patterns.proxy.Proxy;
	
	import uk.co.revisual.pivot.mediators.PivotMediator;
	import uk.co.revisual.pivot.vos.PivotVO;
	

	
	public class PivotProxy extends Proxy
	{

		public static const NAME:String = "PivotProxy";
		
		private var interests:Array;
		private var map:Array;

		public function PivotProxy( ):void{
			super( NAME );
		}
		
		override public function setData( data:Object ):void{
			super.setData( checkData( data ) );
			facade.registerMediator( new PivotMediator( this ) );
		}
		
		private function checkData( data:Object ):Object{
			var list:XMLList
			if( data is XML ){
				return data
			}else if ( data is XMLList ){
				list = XMLList( data )
				if( list.length() == 1)return list[0];
				throw new Error ("only one noteMappings node expected");
			}else{
				throw new Error ("data type not expected");
			}
		}
		
		public function getInterests():Array{
			if( interests )return interests;
			interests = parseInterests();
			return interests;
		}
		
		//todo: impliment state filter 
		public function getPivotList( note:String, state:String ):Array{
			if(!map){
				map = [];
			}else if ( map[note] ){
				return map[note];
			}
			
			map[note] = parseRedirectList(note);
			return map[note];			
			
		}
		
		private function parseRedirectList( note:String ):Array{
			var list:XMLList = XML( data ).elements( "notification" ).( @name == note ).redirect,
				xml:XML,
				vo:PivotVO,
				a:Array = [];
			for each ( xml in list ){
				vo = new PivotVO( xml.@core, xml.@name || note, xml.@type, xml.@state ); 
				a.push( vo );
			}
			return a;
		}
		
		private function parseInterests():Array{
			var list:XMLList = XML( data ).elements( "notification" ),
				xml:XML,
				a:Array = [];
			for each (xml in list ){
				a.push(xml.@name.toString())
			}
			return a;
		}
		

	}
}